package com.nky.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nky.pojo.Files;
import com.nky.service.impl.FilesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/phenotype")
public class PhenotypeController {
    @Autowired
    private FilesServiceImpl filesService;

    @GetMapping("/getFilesByPath")
    public ResponseEntity<Page<JSONObject>> selectAllPhenotype(String filePath, String pageNo, String pageSize) {
        System.out.println("收到路径查询请求"+filePath+" "+pageNo+" "+pageSize);
        // 分页参数
//        Page<Files> page = new Page<>((int) obj.get("pageNo"), (int) obj.get("pageSize"));
        Page<Files> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        // 文件路径
        Page<JSONObject> list;
        if (filePath.equals("/")) {
            list = filesService.getRootPath(page);
        } else {
            // 目录层级等于路径中/的数量加1
            int pathLevel = filePath.split("/").length;
            list = filesService.getFilesByPath(page, pathLevel, filePath);
        }
        System.out.println("list" + list);
        return ResponseEntity.ok(list);
    }
}
